package com.carey.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.carey.yygh.cmn.client.DictFeignClient;
import com.carey.yygh.hosp.repository.HospitalRepository;
import com.carey.yygh.hosp.service.HospitalService;
import com.carey.yygh.model.cmn.Dict;
import com.carey.yygh.model.hosp.Hospital;
import com.carey.yygh.model.hosp.Schedule;
import com.carey.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> switchMap) {
        //将传入的map转换成对象
        String jsonString = JSONObject.toJSONString(switchMap);//先转换成字符串
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        //判断该对象在mangodb中是否存在
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (hospitalByHoscode!=null){
            hospital.setId(hospitalByHoscode.getId());
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospital.setCreateTime(hospitalByHoscode.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> listHosp(Integer current, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(current-1,limit);
        Hospital hospital = new Hospital();
        //hospital.setIsDeleted(0);
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        pages.getContent().forEach(item -> {
            this.sethospital(item);
        });
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //通过id查询获取hospital，在进行更新
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> show(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.sethospital(hospital);
        Map<String,Object> result = new HashMap<>();

//单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
//不需要重复返回
        hospital.setBookingRule(null);

        result.put("hospital", hospital);

        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";

    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    //根据医院编号查询医院详细信息
    @Override
    public Map<String, Object> item(String hoscode) {
        Hospital hospital = this.getByHoscode(hoscode);
        this.sethospital(hospital);
        Map<String,Object> map = new HashMap<>();
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        map.put("hospital",hospital);
        return map;
    }


    public void sethospital(Hospital hospital){
        String name = dictFeignClient.getName("Hostype", hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("address",provinceString+cityString+districtString);
        hospital.getParam().put("name",name);
    }

}
